﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.ComponentModel;
#if !XBOX360
using System.Windows.Forms;
#endif
using Microsoft.Xna.Framework;
using MinimizeEngine.Managers;
using MinimizeEngine.Entities;
#endregion

namespace MinimizeEngine
{
    public partial class MEForm : Form
    {
        #region Properties

        private MEBaseEntity mEntity;

        /// <summary>
        /// Master List of all Nodes in the Scene Tree
        /// </summary>
        private List<TreeNode> mMasterNodeList = new List<TreeNode>();

        /// <summary>
        /// Refresh the list of Nodes from the Scene Graph
        /// </summary>
        public void RefreshNode(TreeNode n)
        {
            //First clear all Nodes in the Tree
            n.Nodes.Clear();
            GetNodes(n);
        }

        /// <summary>
        /// Get the List of all Children Nodes for this Node
        /// </summary>
        /// <param name="treeNode"></param>
        /// <param name="node"></param>
        private void GetNodes(TreeNode treeNode)
        {
            MEBaseEntity node = MESceneGraphManager.FindEntity(treeNode.Text);

            if(node.HasGotChildren)
            {
                mSceneTree.BeginUpdate();
                foreach (MEBaseEntity entity in node.Children)
                {
                    TreeNode tn = new TreeNode(entity.UniqueName);
                    treeNode.Nodes.Add(tn);
                    mMasterNodeList.Add(tn);
                    mEntity = entity;
                }
                mSceneTree.EndUpdate();
            }           
        }

        /// <summary>
        /// Check to see if we have already got the children for this Node
        /// </summary>
        /// <param name="node"></param>
        private bool CheckHasNodes(TreeNode node)
        {
            if(node.Nodes.Count > 0)
            {
                return true;
            }
            return false;
        }

        private TreeNode FindTreeNode(string name)
        {
            foreach (TreeNode node in mMasterNodeList)
            {
                if(name == node.Text)
                {
                    return node;
                }
            }
            return null;
        }

        public void ViewStats(MEBaseEntity entity)
        {
            txSceneNodeName.Text = entity.Name;
            txSceneNodePosition.Text = entity.Position.ToString();
            txBoundingSphereRadius.Text = entity.BoundingSphere.Radius.ToString();
            txBSC.Text = entity.BoundingSphere.Center.ToString();

            if (entity.Parent != null)
            {
                txSceneNodeParent.Text = entity.Parent.Name;
            }
            else
            {
                txSceneNodeParent.Text = "Null";
            }
        }

        #endregion

        #region Initialization

        public MEForm()
        {
            InitializeComponent();
        }

        private void MEForm_Load(object sender, EventArgs e)
        {
            //Root Node
            TreeNode root = new TreeNode(MESceneGraphManager.RootEntity.UniqueName);
            mSceneTree.Nodes.Add(root);
            GetNodes(root);

            int xPos = MEEngineManager.Game.Window.ClientBounds.Location.X + MEEngineManager.Game.Window.ClientBounds.Width + 10;
            int yPos = MEEngineManager.Game.Window.ClientBounds.Location.Y - 30;

            this.SetDesktopLocation(xPos, yPos);
        }

        #endregion

        #region Event Handlers

        private void mSceneTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //Get the Selected Node
            MEBaseEntity selected = MESceneGraphManager.FindEntity(mSceneTree.SelectedNode.Text);

            //Get any Children Nodes this node may have
            RefreshNode(mSceneTree.SelectedNode);

            //View the Selected Nodes Entity Stats
            ViewStats(selected);
        }

        private void MEForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            MEEngineManager.Form = null;
            MEEngineManager.SceneGraphViewerActive = false;
        }

        private void mSceneTree_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            mSceneTree.SelectedNode.Nodes.Clear();
            GetNodes(mSceneTree.SelectedNode);
        }

        #endregion
    }
}
